package com.itlong.cloud.utils.rabbitMQ;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.enumerate.MessageQueueStatus;
import com.itlong.cloud.utils.node.JsonUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <desc>
 *      消息队列帮助类。
 * </desc>
 *
 * @createDate 2017/9/8
 */
public class MessageSendUtil {
    private static final Logger LOG = LoggerFactory.getLogger(MessageSendUtil.class);
    /**
     * <desc>
     *      发送消息，不需要实现任何接口，供外部调用。
     * <desc/>
     *
     * @param queueName         队列名称
     * @param msg                消息内容
     * @param exchange          交互机
     * @param routingKey        路由key
     * @param rabbitTemplate   rabbit实例
     * @author Wenchao.L
     * @createDate
     */
    public static void send(
            String queueName,
            String msg,
            String exchange,
            String routingKey,
            RabbitTemplate rabbitTemplate) throws Exception {

        try {
            CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = new TimeTaskDeadMessageQueuePO();
            timeTaskDeadMessageQueuePO.setConsumerNum(0);
            timeTaskDeadMessageQueuePO.setCorrelationId(correlationId.toString());
            timeTaskDeadMessageQueuePO.setExchange(exchange);
            //设置消息内容时先从内容中取出 correlationId 如果为null，设置correlationId
            Map<String,Object> map;
            try{
                map = JsonUtil.toObject(msg, HashMap.class);
                if(map.get("correlationId")==null){
                    map.put("correlationId",timeTaskDeadMessageQueuePO.getCorrelationId());
                }
            }catch (Exception e){
                throw new Exception(String.format("【消息队列_发送消息失败！msg结构转换失败】msg:%s   queueName:%s",msg,queueName),e);
            }
            timeTaskDeadMessageQueuePO.setMessageContent(JsonUtil.toJSON(map));
            timeTaskDeadMessageQueuePO.setRoutingKey(routingKey);
            timeTaskDeadMessageQueuePO.setQueueName(queueName);
            timeTaskDeadMessageQueuePO.setMessageStatus(MessageQueueStatus.NOT_CONSUMER.getType());
            timeTaskDeadMessageQueuePO.setCreateTime(new Date());
            timeTaskDeadMessageQueuePO.setStatus(1);
            Message message = MessageBuilder.withBody(JsonUtil.toJSON(timeTaskDeadMessageQueuePO).getBytes())
                    .setContentType(MessageProperties.CONTENT_TYPE_JSON).setMessageId(correlationId.toString())
                    .build();
            rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationId);
        } catch (Exception ex) {
            LOG.error("【发送消息到MQ失败】{}", msg);
            throw new Exception("【消息队列_发送消息失败！】:", ex);
        }
    }
}
